package org.finmanager.core;

import com.alee.extended.panel.WebButtonGroup;
import com.alee.laf.button.WebButton;
import org.finmanager.data.CategoryList;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * Created by Kelvin on 2/4/2014.
 */

//This class defining the panel design of the favourite boxes shown in the budget function
//On the top left side
//Showing the 5 favourite categories in order to let user immediately add that item 
public class FavouritesPanelUI implements TableModelListener {
	private JPanel panel;
	private ItemManagerController itemManagerController;
	private CategoryList categories;

	public FavouritesPanelUI(JPanel panel, ItemManagerController itemManagerController) {
		this.panel = panel;
		this.itemManagerController = itemManagerController;
	}

	@Override
	public void tableChanged(TableModelEvent e) {
		if (e.getColumn() == CategoryTableModel.FAVOURITE) {
			refreshFavourites();
		}
	}

	private void refreshFavourites() {
		panel.removeAll();
		WebButtonGroup buttonGroup = new WebButtonGroup();

		for (Category category : categories.getFavouriteCategories()) {
			WebButton button = new WebButton(category.getName());
			button.setDrawFocus(false);
			button.setActionCommand(ItemManagerController.OPEN_ITEM_INPUT_UI_ADD_CATEGORY);
			button.addActionListener(itemManagerController);
			buttonGroup.add(button);
		}

		panel.add(buttonGroup);
		panel.updateUI();
	}

	public void setCategories(CategoryList categories) {
		this.categories = categories;
		refreshFavourites();
	}
}
